package com.dragon.app.jvm;

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.WeakHashMap;

public class ReferenceTest {

	public static void main(String[] args) {

		// 强引用不会被回收
		Object object = new Object();
		Object object2 = object;
		object = null;
		System.out.println(object2);

		// 软引用，内存够用时，对象不会被回收
		Object object3 = new Object();
		Reference<Object> softReference = new SoftReference<Object>(object3);
		System.out.println("softReference: " + softReference.get());
		object3 = null;
		System.gc();
		System.out.println("softReference before OOM: " + softReference.get());
		try {
			// 模拟OOM
//			byte[] bytes = new byte[30 * 1024 * 1024];
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("softReference after OOM: " + softReference.get());
		}
		
		WeakReference weakReference;
		WeakHashMap weakHashMap = new WeakHashMap<>();
		ThreadLocal threadLocal;
		Hashtable hashtable;
		HashMap hashMap;
		

	}

}
